package com.fessc.develop.handler.processor.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fessc.develop.base.Result;
import com.fessc.develop.entity.dbvo.TBankCode;
import com.fessc.develop.model.enums.EnumJsonProperty;
import com.fessc.develop.model.enums.EnumResultType;
import com.fessc.develop.model.enums.EnumServiceCode;
import com.fessc.develop.service.interfaces.ITBankCodeService;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ComPlatInfoSyncProcess implements IBaseService {

    @Resource
    private ITBankCodeService bankCodeService;

    @Override
    public String getServiceCode() {
        return EnumServiceCode.COM_BS0006.toString();
    }

    @Override
    public Result process(JSONObject requestBody) {
        log.info("市场交易平台信息同步请求报文: " + requestBody.toJSONString());
        JSONObject body = requestBody.getJSONObject(EnumJsonProperty.REQ_BODY.toString());
        String bankId = body.getString("bankId");
        String orgCode = body.getString("orgCode");
        String appId = body.getString("appId");
        String option = body.getString("option");
        List<TBankCode> bankCodeList = bankCodeService.geOrgCodeByAppId(bankId, appId, "0");
        if (BusiUtil.isEquals("1", option)) {  //新增
            if (BusiUtil.isNull(bankCodeList) || bankCodeList.isEmpty()) {
                TBankCode bankCode = new TBankCode();
                bankCode.setBankId(bankId);
                bankCode.setOrgCode(orgCode);
                bankCode.setUpdateDate(DateUtil.getCurrentLocalDateTime());
                bankCode.setAppId(appId);
                bankCode.setOrgStatus("0");
                bankCode.setAppType("0");
                bankCodeService.save(bankCode);
            } else if (bankCodeList.size() == 1){  //修改
                bankCodeService.updateBankCodeByOrgCode(bankId, orgCode, appId, "");
            }
        } else if (BusiUtil.isEquals("2", option)){  //修改
            bankCodeService.updateBankCodeByOrgCode(bankId, orgCode, appId, "");
        } else if (BusiUtil.isEquals("3", option)) {  //删除
            bankCodeService.updateBankCodeByOrgCode(bankId, orgCode, appId, "1");
        }
        Result result = new Result(EnumResultType.SUCCESS.getStatusCode(), EnumResultType.SUCCESS.getMsg());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(EnumJsonProperty.RES_CODE.toString(), EnumResultType.SUCCESS.getStatusCode());
        jsonObject.put(EnumJsonProperty.RES_MSG.toString(), EnumResultType.SUCCESS.getMsg());
        result.setData(jsonObject);
        log.info("市场交易平台信息同步返回报文: " + JSON.toJSONString(result));
        return result;
    }
}
